﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Activity_SignUpActivity : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.QueryString["activityid"]))
        {
            Response.Redirect("~/home/default.aspx");
        }
        else
        {
            int activityID = 0;
            int.TryParse(Request.QueryString["activityid"], out activityID);
            if (ActivityRules.Instance.IsActivitySignUpFinished(activityID))
            {
                Response.Redirect("~/Activity/detail.aspx?activityId=" + activityID);
            }
            else
            {
                if (!IsPostBack)
                {
                    User user = UserRules.Instance.GetCurrentUser();
                    lb_Name.Text = user.FullName;
                    DateTime birthday = user.Birthday.Value;
                    int age = DateTime.Today.Year - birthday.Year;
                    lb_Age.Text = age.ToString();
                    if (!UserRules.Instance.UserContactInfoExisted(user.UserId))
                    {
                        if (!string.IsNullOrEmpty(user.UsersContact.MobilePhone))
                        {
                            lb_Phone.Text = user.UsersContact.MobilePhone;
                        }
                        else if (!string.IsNullOrEmpty(user.UsersContact.Phone))
                        {
                            lb_Phone.Text = user.UsersContact.Phone;
                        }
                        else
                        {
                            lb_Phone.Text = "还未填写联系方式,请到个人设置中补全，以便组织者更方便的联系到您";
                        }
                    }
                    else
                    {
                        lb_Phone.Text = "还未填写联系方式,请到个人设置中补全，以便组织者更方便的联系到您";
                    }
                    if (!UserRules.Instance.UserDetailInfoExisted(user.UserId))
                    {
                        if (!string.IsNullOrEmpty(user.UsersDetail.Dictionary2.ObjectValue.ToString()))
                        {
                            lb_Title.Text = user.UsersDetail.Dictionary2.ObjectValue.ToString();
                        }
                        else
                        {
                            lb_Title.Text = "还未在个人详细资料中填写职位";
                        }
                    }
                    else
                    {
                        lb_Title.Text = "还未填写个人详细资料";
                    }
                }
            }
        }
        bt_Sign.Click += new EventHandler(bt_Sign_Click);
    }

    void bt_Sign_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["activityid"]))
        {
            int activityId = Convert.ToInt32(Request.QueryString["activityid"]);
            if (!ActivityRules.Instance.IsSignedActivity(UserRules.Instance.GetCurrentUserId(), activityId))
            {
                int man = 0, woman = 0;
                if (!string.IsNullOrEmpty(txt_Man.Text.Trim()))
                {
                    man = Convert.ToInt32(txt_Man.Text);
                }
                if (!string.IsNullOrEmpty(txt_Woman.Text.Trim()))
                {
                    woman = Convert.ToInt32(txt_Woman.Text);
                }
                if (string.IsNullOrEmpty(txt_Man.Text.Trim()) && string.IsNullOrEmpty(txt_Woman.Text.Trim()))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('报名人数不能为空');", true);
                }
                else
                {
                    int curJoinedPeopleCount = ActivityRules.Instance.GetJoinedUserNum(activityId);
                    int SignUpLimit = ActivityRules.Instance.GetActivityInfo(activityId).SignUpLimit;
                    int singleUserSignUpLimit = ActivityRules.Instance.GetActivityInfo(activityId).SingleUserSignUpLimit;
                    if ((man + woman) > singleUserSignUpLimit)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('每位用户限报" + singleUserSignUpLimit.ToString() + "人');", true);
                    }
                    else if ((man + woman) > (SignUpLimit - curJoinedPeopleCount))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('该活动还可报名" + (SignUpLimit - curJoinedPeopleCount).ToString() + "人');", true);
                    }
                    else
                    {
                        ActivityRules.Instance.SignUpActivity(UserRules.Instance.GetCurrentUserId(), activityId, woman, man);
                        if (ActivityRules.Instance.IsActivityNeedPermit(activityId))
                            ActivityRules.Instance.SendMemberAddRequest(activityId, UserRules.Instance.GetCurrentUserId());
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('已成功报名该活动'); window.location.href = '/Activity/detail.aspx?activityid="+activityId+"';", true);
                    }
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('您已报名该活动，不能重复报名');", true);
            }
        }
    }
}
